Parse.initialize("bnGFk9tGYPR4V8Pr051GOkAwqKdVNVa3vT5AnocK", "i8jh3wmNjGikRbWvoiwFxdIn9DO454jpo5VNeLGJ");

var Article = Parse.Object.extend("Article");
var Body=Parse.Object.extend("ArticleBody");
var Post=Parse.Object.extend("Post");

function getArticleList(cb){
	var query=new Parse.Query(Post);
	query.descending("createdAt");
	query.equalTo("type",3);
    query.find({
        success:function(results){
        	cb(results)
        },
        error:function(error){
           cb(null)
        }
    })
}

function saveArticle(title,imgUrl,cb){
	var arctice= new Post();
	arctice.set("title",title);
	arctice.set("url",imgUrl);
	arctice.set("type",3);
	arctice.save(null,{
		success:function(p){
			cb(p);	
		},
		error:function(error){
			cb(null);
		}
	})
}

function getArticleInfo(objectId,cb){
	var query=new Parse.Query(Post);
	var bodyQuery=new Parse.Query(Body);
	query.get(objectId,{
		success:function(obj){
			console.dir(obj);
			var post=new Post();
			post.id=objectId;
			
			bodyQuery.equalTo("article",post);
			bodyQuery.include("article");
			//bodyQuery.ascending("createdAt");
			
			bodyQuery.find().then(function(bodys){
				console.dir(bodys);
				cb(null,obj,bodys);
			},function(error){
				console.log(error);
			})

		},
		error:function(obj,error){
			cb(error,null,null);
		}
	})
}

function addArticleBody(articleId, type,url,content,cb){
	var body=new Body();
	console.log(type+','+url+','+content);
	body.set("type",parseInt(type));
	if(url){
		body.set("url",url);
	}
	

	body.set("text",content);
	var post=new Post();
	post.id=articleId;
	body.set("article",post);
	body.save(null,{
		success:function(object){
			console.dir(object);
			cb(null,object);
		},
		error:function(error){
			console.log(error);
			cb(error,null);
		}
	})
}

function updateArcticle(objectId,body,cb){
	var query=new Parse.Query(Article);
	query.get(objectId,{
		success:function(object){
			var ralation=object.relation('body');
			ralation.add(body);
			object.save(null,{
				success:function(saveObj){
					cb(null,saveObj);	
				},
				error:function(error){
					cb(error,null);
				}
			});
		},
		error:function(error){
			cb(error,null);
		}
	})
}

function deleteArcticleBody(articleId,bodyId,cb){
	var bodyQuery=new Parse.Query(Body);

	bodyQuery.get(bodyId,{
		success:function(bodyObject){
			console.dir(bodyObject);
			var query=new Parse.Query(Article);
			query.get(articleId,{
				success:function(object){
					var ralation=object.relation('body');
					ralation.remove(bodyObject);
					object.save(null,{
						success:function(saveObj){
							bodyObject.destroy();
							cb(null,saveObj);
						},
						error:function(error){
							cb(error,null);
						}
					});
				},
				error:function(error){
					cb(error,null);
				}
				
			})
		},
		error:function(error){
			cb(error,null);
		}
	})
}

function savePost(title,url,cb){

	var post= new Post();
	post.set("title",title);
	post.set("url",url);
	post.save(null,{
		success:function(p){

			cb(p);	
		},
		error:function(error){
			cb(null);
		}
	})
}

function getPostInfo(objectId,cb){
	var query= new Parse.Query(Post);
	query.get(objectId,{
		success:function(p){
			cb(null,p);
		},
		error:function(object,error){
			cb(error,null);
		}
	})
};

function updatePost(objectId,title,desc,cb){
	var query=new Parse.Query(Post);
	query.get(objectId,{
		success:function(p){
			p.set('title',title);
			p.set('content',desc);
			p.save(null,{
				success:function(p2){
					cb(null,p2);
				},
				error:function(obj,error){
					cb(error,null);
				}
			});
		},
		error:function(obj,error){
			cb(error,null);
		}
	})
};

function deletePost(objectId,cb){
	var query=new Parse.Query(Post);
	query.get(objectId,{
		success:function(p){
			p.destroy({
				success:function(delObj){
					cb(null,delObj);
				},
				error:function(obj,error){
					cb(error,null);
				}
			})
		}
	})
}

function test(articleId,bodyId,cb){
	cb(bodyId);
}